   package com.javalong.affairs.module.notifything.activity;

import java.util.HashMap;
import java.util.Map;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.javalong.affairs.R;
import com.javalong.affairs.base.activity.UIHandlerActivity;
import com.javalong.affairs.module.userModule.adapter.UserStatisticAdapter;
import com.javalong.affairs.base.constant.BundleKey;
import com.javalong.affairs.base.constant.DataType;
import com.javalong.affairs.base.constant.RequestKey;
import com.javalong.affairs.base.data.AllData;
import com.javalong.affairs.base.listener.BaseResponseListener;
import com.javalong.affairs.base.model.MessageInfo;
import com.javalong.affairs.base.model.MessageStatistic;
import com.javalong.affairs.base.model.User;
import com.javalong.affairs.base.ui.TopTitle;
import com.javalong.affairs.base.utils.FileUtils;
import com.javalong.affairs.base.utils.HttpUtils;
import com.javalong.affairs.base.utils.StringUtils;
import com.javalong.affairs.base.utils.TimeUtils;
import com.javalong.affairs.base.utils.UrlUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;

@ContentView(R.layout.activity_msgstatistic)
public class MsgStatisticActivity extends UIHandlerActivity implements
		OnItemClickListener {
	@ViewInject(R.id.tv_time)
	private TextView tv_time;
	@ViewInject(R.id.tv_content)
	private TextView tv_content;
	@ViewInject(R.id.ui_toptitle)
	private TopTitle topTitle;
	@ViewInject(R.id.lv_statistic)
	private ListView lv_statistic;
	@ViewInject(R.id.bt_statistic)
	private Button bt_statistic;
	@ViewInject(R.id.tv_statistic_content)
	private TextView tv_statistic_content;

	private String msgId;
	private String simpleMsgId;
	private MessageStatistic msgStatistic;
	public UserStatisticAdapter adapter;
	public String downloadFilePath;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ViewUtils.inject(this);
		topTitle.setOnLeftDrawableClick(this);
		Bundle bundle = getIntent().getBundleExtra(BundleKey.BUNDLE);
		msgId = bundle.getString(BundleKey.MSGID);
		simpleMsgId = bundle.getString(BundleKey.SIMPLEMSGID);
		msgStatistic = new MessageStatistic();
		adapter = new UserStatisticAdapter(this, msgStatistic.userList);
		lv_statistic.setAdapter(adapter);
		bt_statistic.setOnClickListener(this);
		lv_statistic.setOnItemClickListener(this);
		initData();
	}

	private void initData() {
		User user = (User) AllData.GetInstance().get(
				R.string.data_user_current_user);
		String url = UrlUtils.getUrl("SendMessage",
				"outList_message_allMessage");
		Map<String, Object> params = new HashMap<String, Object>();
		params.put(RequestKey.ID, simpleMsgId);
		HttpUtils.post(url, params, new BaseResponseListener(
				DataType.MESSAGESTATISTIC) {
			@Override
			public void onSuccess(Object obj) {
				super.onSuccess(obj);
				MessageStatistic mstatistic = (MessageStatistic) obj;
				initTopUI(mstatistic);
				if (mstatistic.affirmList != null) {
					msgStatistic.userList.addAll(mstatistic.affirmList);
				}
				if (mstatistic.entrustList != null) {
					msgStatistic.userList.addAll(mstatistic.entrustList);
				}
				if (mstatistic.leaveList != null) {
					msgStatistic.userList.addAll(mstatistic.leaveList);
				}
				if (mstatistic.waitList != null) {
					msgStatistic.userList.addAll(mstatistic.waitList);
				}
				tv_statistic_content.setText("总共：" + mstatistic.all + "人  未读："
						+ mstatistic.wait + "人 确认：" + mstatistic.affirm
						+ "人 请假：" + mstatistic.leave + "人 委托："
						+ mstatistic.entrust);
				adapter.notifyDataSetChanged();
			}
		});
	}

	@Override
	public void refresh() {
		super.refresh();
	}

	private void initTopUI(MessageStatistic mstatistic) {
		tv_time.setText(TimeUtils.getFormatTime2(mstatistic.info.time));
		tv_content.setText(mstatistic.info.title);
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		switch (v.getId()) {
		case R.id.bt_statistic:
			Map<String, Object> params = new HashMap<String, Object>();
			params.put(RequestKey.ID, simpleMsgId);
			String url = UrlUtils.getUrl("SendMessage",
					"excel_message_allMessage");
			HttpUtils.post(url, params, new BaseResponseListener(
					DataType.MESSAGE) {
				@Override
				public void onSuccess(Object obj) {
					MessageInfo info = (MessageInfo) obj;
					downloadFilePath = StringUtils.getRandomFilePath();
					FileUtils
							.download(bt_statistic, info.url, downloadFilePath);
				}
			});
			break;
		}

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {

	}

}
